public class ExprExercise {

    public static void main(String[] args) {
        ExprExercise exprExercise = new ExprExercise();
        int expr = exprExercise.expr("-1*3+(2-(2+1))*3");
        System.out.println("expr = " + expr);
//
        System.out.println("=================user case :====================");
        System.out.println("=================user case : expr(\"1\") == 1 = " +  (exprExercise.expr("1") == 1));
        System.out.println("=================user case : expr(\"1+2\") == 3 = " +  (exprExercise.expr("1+2") == 3));
        System.out.println("=================user case : expr(3-2) == 1 = " +  (exprExercise.expr("3-2") == 1));
        System.out.println("=================user case : expr(\"1+2+3\") == 6 = " +  (exprExercise.expr("1+2+3") == 6));
        System.out.println("=================user case : expr(\"(1+2)-3\") == 0 = " +  (exprExercise.expr("(1+2)-3") == 0));
        System.out.println("=================user case : expr(1+2*3) == 7 = " +  (exprExercise.expr("1+2*3") == 7));
        System.out.println("=================user case : expr(\"((2-1)-2)*3+(2-(2+1))*3\") == -6 = " +  (exprExercise.expr("((2-1)-2)*3+(2-(2+1))*3") == -6));
    }

    public int expr(String input) {
        return parenthesisOpt(input.replaceAll(" ",""));
    }

    public int parenthesisOpt(String opt) {
        if (opt.contains("(") ) {
            if (opt.indexOf("(") == 0) {
                int indexRight = opt.indexOf("(");
                int indexLeft = opt.lastIndexOf(")");
                int num = 0;
                for (int i = 0; i < opt.length(); i++) {
                    String chr = opt.charAt(i) + "";
                    if (chr.equals("(")) {
                        num++;
                    }
                    if (chr.equals(")")) {
                        num--;
                        if (num == 0) {
                            indexLeft = i;
                            break;
                        }
                    }
                }
                String a = opt.substring(indexRight + 1, indexLeft);
                if (indexLeft + 1 == opt.length()) {
                    return parenthesisOpt(a);
                } else {
                    String b = opt.substring(indexLeft + 1);
                    return parenthesisOpt(parenthesisOpt(a)+ b);
                }

            } else {
                String a = opt.substring(0, opt.indexOf("("));

                int indexRight = opt.indexOf("(");
                int indexLeft = opt.lastIndexOf(")");
                int num = 0;
                for (int i = 0; i < opt.length(); i++) {
                    String chr = opt.charAt(i) + "";
                    if (chr.equals("(")) {
                        num++;
                    }
                    if (chr.equals(")")) {
                        num--;
                        if (num == 0) {
                            indexLeft = i;
                            break;
                        }
                    }
                }

                String b = opt.substring(indexRight + 1, indexLeft);
                String exp = "";
                if (indexLeft + 1 == opt.length()) {
                    exp = a + parenthesisOpt(b);
                } else {
                    String c = opt.substring(indexLeft+1);
                    exp = a + parenthesisOpt(b) + c;
                }
                exp = exp.replaceAll("--","+")
                        .replaceAll("\\+-","-")
                        .replaceAll("\\*-","-")
                        .replaceAll("\\/-","-")
                ;
                return opt(exp);
            }
        } else {
            return opt(opt);
        }
    }

    public int opt(String exp) {
        if (exp.indexOf("+") != -1) {
            String a = exp.substring(0,exp.indexOf("+"));
            String b = exp.substring(exp.indexOf("+") + 1);
            if (a.indexOf("-") == -1 && a.indexOf("*") == -1 && a.indexOf("/") == -1) {
                return Integer.parseInt(a) + opt(b);
            } else {
                return opt(a) + opt(b);
            }
        } else if (exp.length()>1 && exp.substring(1).indexOf("-") != -1 ) {
            String a = "";
            String b = "";
            if (exp.startsWith("-")) {
                a = "-" + exp.substring(1).substring(0,exp.substring(1).indexOf("-"));
                b =  exp.substring(1).substring(exp.substring(1).indexOf("-") + 1);
            } else {
                a = exp.substring(0,exp.indexOf("-"));
                b =  exp.substring(exp.indexOf("-") + 1);
            }
            if (a.indexOf("-") == -1 && a.indexOf("*") == -1 && a.indexOf("/") == -1) {
                return Integer.parseInt(a) - opt(b);
            }else {
                return opt(a) - opt(b);
            }
        } else if (exp.indexOf("*") != -1) {
            String a = exp.substring(0,exp.indexOf("*"));
            String b = exp.substring(exp.indexOf("*") + 1);
            if (a.indexOf("-") == -1 && a.indexOf("*") == -1 && a.indexOf("/") == -1) {
                return Integer.parseInt(a) * opt(b);
            }else {
                return opt(a) * opt(b);
            }
        } else if (exp.indexOf("/") != -1) {
            String a = exp.substring(0,exp.indexOf("/"));
            String b = exp.substring(exp.indexOf("/") + 1);
            if (a.indexOf("-") == -1 && a.indexOf("*") == -1 && a.indexOf("/") == -1) {
                return (int) (Integer.parseInt(a) *1.0 / opt(b));
            }else {
                return (int) (opt(a) *1.0 / opt(b));
            }
        }
        return Integer.parseInt(exp);
    }

}
